/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/

#include "zb32l03x_hal.h"
#include "bsp_beep.h"
static  TIM_HandleTypeDef           TIMx = {0};

/*
*********************************************************************************************************
*	功能说明: adc初始化
*	形    参: 
*	返 回 值: 
*   说    明：
*********************************************************************************************************
*/
void bsp_beep_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    TIM_OC_InitTypeDef          TIMxOC = {0};

    BSP_BEEP_CLKENABLE(); 
    BSP_BEEP_TIMBASE_CLK_ENABLE();   

    GPIO_InitStruct.Pin             = BSP_BEEP_PIN;
    GPIO_InitStruct.Mode            = GPIO_MODE_AF;
    GPIO_InitStruct.Pull            = GPIO_PULLUP;
    GPIO_InitStruct.OpenDrain       = GPIO_PUSHPULL;
    GPIO_InitStruct.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStruct.SlewRate        = GPIO_SLEW_RATE_HIGH;
    GPIO_InitStruct.DrvStrength     = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStruct.Alternate       = BSP_BEEP_FUN;
    HAL_GPIO_Init(BSP_BEEP_PORT, &GPIO_InitStruct);

   
    TIMx.Instance               = BSP_BEEP_TIMBASE;
    TIMx.Init.Period            = BSP_BEEP_PERIOD;                             // TIM1A_ARR Period
    TIMx.Init.Prescaler         = (uint32_t)(HAL_RCC_GetPCLKFreq() / 1000000) - 1;                             // TIMx Frequency(CK_CNT) = fCK_PSC/(PSC[15:0]+1) = TIMx_FREQ
    TIMx.Init.ClockDivision     = 0;                               // CKD(Clock division)
    TIMx.Init.CounterMode       = TIM_COUNTERMODE_UP;              // Edge alignment mode ,counter up
    TIMx.Init.RepetitionCounter = 0;                               // TIM1A_RCR
    TIMx.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;   // Enable Auto-reload
    HAL_TIM_PWM_Init(&TIMx);

    TIMxOC.OCMode     = TIM_OCMODE_PWM1;                            // PWM mode1 TIM1A_CCMR1
    TIMxOC.Pulse      = BSP_BEEP_PERIOD/2;                          // This value is used to determine the duty cycle,and the default duty cycle is 50%
    TIMxOC.OCPolarity = TIM_OCPOLARITY_HIGH;                        // Output polarity, OC1 is valid at low level

    HAL_TIM_PWM_ConfigChannel(&TIMx, &TIMxOC, BSP_BEEP_PWM_CHANNEL); // Configure TIM1A Channel2
   
    HAL_TIM_PWM_Stop(&TIMx, BSP_BEEP_PWM_CHANNEL); //Open PWM Channel2  
    

}

/**
  * @brief  打开蜂鸣器
  * @param  
  * @param  
  * @retval NUll
  */

void bsp_beep_open(void)
{
   HAL_TIM_PWM_Start(&TIMx, BSP_BEEP_PWM_CHANNEL); //Open PWM Channel2  
}
/**
 * @brief  打开蜂鸣器
 * @param
 * @param
 * @retval NUll
 */

void bsp_beep_close(void)
{
   HAL_TIM_PWM_Stop(&TIMx, BSP_BEEP_PWM_CHANNEL); //Open PWM Channel2  
}
/**
  * @brief  设置频率
  * @param  
  * @param  
  * @retval NUll
  */
 void bsp_beep_setfreq(uint32_t freq)
 {
    
 }




